const pool = require('../database');
// 通用错误处理函数
const handleError = (error, res) => {
    console.error(error);
    res.status(500).json({ message: '操作失败，请稍后重试' });
};

// 通用查询函数
const executeQuery = async (query, params = []) => {
    try {
        const [result] = await pool.execute(query, params);
        return result;
    } catch (error) {
        throw error;
    }
};


// 格式化日期时间为 yyyy-mm-dd hh:mm:ss
const formatDateTime = (date) => {
    const d = new Date(date);
    const year = d.getFullYear();
    const month = (d.getMonth() + 1).toString().padStart(2, '0');
    const day = d.getDate().toString().padStart(2, '0');
    const hours = d.getHours().toString().padStart(2, '0');
    const minutes = d.getMinutes().toString().padStart(2, '0');
    const seconds = d.getSeconds().toString().padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

module.exports = {
    handleError,
    executeQuery,
    formatDateTime
};
